package com.wef.system.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.gfa4j.exception.ValidateRuntimeException;
import com.gfa4j.hibernate.BaseObject;
import com.gfa4j.query.Page;
import com.gfa4j.query.QueryInfo;
import com.gfa4j.service.impl.BaseServiceImpl;
import com.gfa4j.web.SessionManager;
import com.gfa4j.web.UserDetail;
import com.wef.system.entity.Office;
import com.wef.system.entity.Role;
import com.wef.system.entity.domain.OfficeDomain;
import com.wef.system.entity.domain.RoleDomain;
import com.wef.system.service.RoleService;

/**
 * @FileName RoleServiceImpl.java
 * @Summary  功能描述：
 * @Author   KOO
 * @Version  1.0
 * @Date     2009/07/31
 * @example  使用示例：略。
 */

public class RoleServiceImpl extends BaseServiceImpl implements RoleService {
	
	private Office getOfficeByCode(String officeCode){
		List offices = (List) findByProperty(Office.class,OfficeDomain.officeCode, officeCode);
		if (!offices.isEmpty()){
			return (Office)offices.get(0);
		}
		return null;
	}
	public Role addRole(Role role){
		
		Office office = getOfficeByCode(role.getOfficeCode());
		if (office != null){
			role.setOfficeId(office.getOfficeId());
		}else{
			UserDetail userDetail = SessionManager.getUserDetail();
			userDetail.getOfficeCode();
			Office currentUserOffice = getOfficeByCode(role.getOfficeCode());
			if (currentUserOffice != null){
				role.setOfficeId(currentUserOffice.getOfficeId());
			} else {
				QueryInfo info = new QueryInfo(Office.class);
				info.setPageSize(1);
				info.setListType(QueryInfo.LISTTYPE_PAGE);
				Page pageOffice = (Page)findByQuery(info);
				if(pageOffice.getList() != null && pageOffice.getList().size() > 0){
					Office firstOffice = (Office)pageOffice.getList().get(0);
					role.setOfficeId(firstOffice.getOfficeId());
				}
			}
		}

		return (Role) super.saveObject(role);
	}

	public void deleteRole(Role role) {		
		super.deleteObject(role);
		
	}

	public Role saveOrUpdateRole(Role role){	
		if (role.getRowStatus() == BaseObject.ROWSTATUS_ADDED) {
			return this.addRole(role);
		}
		if (role.getRowStatus() == BaseObject.ROWSTATUS_MODIFIED) {
			return this.updateRole(role);
		}
		if (role.getRowStatus() == BaseObject.ROWSTATUS_DELETED) {
			this.deleteRole(role);
			return null ;
		}
		return role;
	}

	public List<Role> saveOrUpdateRoles(List<Role> roles){
		StringBuilder errorString = new StringBuilder();
		List<Role> newEntitys = new ArrayList<Role>();
		for (Role role : roles) {
			try {
				role = saveOrUpdateRole(role);
				newEntitys.add(role);
			} catch (ValidateRuntimeException ex) {
				errorString.append(ex.getMessage() + "\n");
			}
		}
		if (errorString.length() > 0) {
			throw new ValidateRuntimeException(errorString.toString());
		}
		return newEntitys;
	}

	public Role updateRole(Role role) {
		Office office = getOfficeByCode(role.getOfficeCode());
		if (office != null) {
			role.setOfficeId(office.getOfficeId());
		} else {
			UserDetail userDetail = SessionManager.getUserDetail();
			userDetail.getOfficeCode();
			Office currentUserOffice = getOfficeByCode(role.getOfficeCode());
			if (currentUserOffice != null){
				role.setOfficeId(currentUserOffice.getOfficeId());
			} else {
				QueryInfo info = new QueryInfo(Office.class);
				info.setPageSize(1);
				info.setListType(QueryInfo.LISTTYPE_PAGE);
				Page pageOffice = (Page)findByQuery(info);
				if(pageOffice.getList() != null && pageOffice.getList().size() > 0){
					Office firstOffice = (Office)pageOffice.getList().get(0);
					role.setOfficeId(firstOffice.getOfficeId());
				}
			}
		}
		return (Role) super.updateObject(role);
	}	
	
	public boolean validateRoleNameExist(String roleName, String roleId){
		StringBuffer hql = new StringBuffer("select role from Role as role where role.roleName = :roleName");
		Map<String, Object> queryParam = new HashMap<String, Object>();
		queryParam.put(RoleDomain.roleName, roleName);
		if (StringUtils.isNotBlank(roleId)) {
			hql.append(" and role.roleId <> :roleId");
			queryParam.put(RoleDomain.roleId, roleId);
		}
		List lst = findByNamedParam(hql.toString(), queryParam);
//		List lst = findByProperty(Role.class, RoleDomain.roleName, roleName);
		if(lst.size() > 0){
			return true;
		}
		return false;
	}
	public Object testTransaction() throws Exception{		
		
		Role role = new Role();
		role.setRoleId("2c9081e52a17c357012a17c54d3b0011");
		role.setRoleDesc("test");		
		role = (Role) this.updateObject(role);
//		if(role!=null){		
//			throw new Exception("事务测试，返回异常！");
//		}
		return role;
	}
}
